<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Global_mdl extends CI_Model 
{
    var $table = 'article';
	const DB_CAIPU = 'caipu';
	public function __construct()
	{
		parent::__construct();
	}


	public function get($data = NULL, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
	{
		$this->db->select('*');
		if($data)
		{
			$this->db->where($data);
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get($this->table);
	}

    public function get_by_id($id)
    {
    	$data = array();
		$this->db->select('*')->from($this->table)->where('id', intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			//$data['addtime'] = date("Y-m-d h:i", $data['addtime']);
		}

		$query->free_result();
		
		return $data;
    }

    public function get_by_($id, $name = 'id')
    {
    	$data = array();
		$this->db->select('*')->from($this->table)->where($name, intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			//$data['addtime'] = date("Y-m-d h:i", $data['addtime']);
		}

		$query->free_result();
		
		return $data;
    }
    public function update_views($id)
    {
		$this->db->set('views','views+1',FALSE);
		$this->db->where('id',$id);
    	return $this->db->update($this->table);
    }

	public function insert($data)
	{
		return $this->db->insert($this->table,$data);
	}

	public function update($data, $id)
	{
		return $this->db->update($this->table, $data, 'id = '.$id);
	}

	public function delete($id)
	{
		return $this->db->where('id',$id)->delete($this->table);
	}

	public function delete_by($idname, $id)
	{
		return $this->db->where($idname,$id)->delete($this->table);
	}
}

